<!--suppress ALL -->
<include file="./Template/header.html" />

<script type="text/javascript">


	$(function() {

		//初始化页面配置
		initPage("希卫康");

		var app = new Vue({
			el: '#container',
			data: {
				voice: []
			},
			methods: {

			}
		})

		// 4 音频接口
		// 4.2 开始录音
		document.querySelector('#startRecord').onclick = function () {
			wx.startRecord({
				cancel: function () {
					alert('用户拒绝授权录音');
				}
			});
		};

		// 4.3 停止录音
		document.querySelector('#stopRecord').onclick = function () {
			wx.stopRecord({
				success: function (res) {
					app.voice.localId = res.localId;
				},
				fail: function (res) {
					alert(JSON.stringify(res));
				}
			});
		};

		// 4.4 监听录音自动停止
		wx.onVoiceRecordEnd({
			complete: function (res) {
				app.voice.localId = res.localId;
				alert('录音时间已超过一分钟');
			}
		});

		// 4.5 播放音频
		document.querySelector('#playVoice').onclick = function () {
			if (app.voice.localId == '') {
				alert('请先使用 startRecord 接口录制一段声音');
				return;
			}
			wx.playVoice({
				localId: app.voice.localId
			});
		};

		// 4.6 暂停播放音频
		document.querySelector('#pauseVoice').onclick = function () {
			wx.pauseVoice({
				localId: app.voice.localId
			});
		};

		// 4.7 停止播放音频
		document.querySelector('#stopVoice').onclick = function () {
			wx.stopVoice({
				localId: app.voice.localId
			});
		};

		// 4.8 监听录音播放停止
		wx.onVoicePlayEnd({
			complete: function (res) {
				alert('录音（' + res.localId + '）播放结束');
			}
		});

		// 4.8 上传语音
		document.querySelector('#uploadVoice').onclick = function () {
			if (app.voice.localId == '') {
				alert('请先使用 startRecord 接口录制一段声音');
				return;
			}
			wx.uploadVoice({
				localId: app.voice.localId,
				success: function (res) {
					alert('上传语音成功，serverId 为' + res.serverId);
					app.voice.serverId = res.serverId;
				}
			});
		};

		// 4.9 下载语音
		document.querySelector('#downloadVoice').onclick = function () {
			if (app.voice.serverId == '') {
				alert('请先使用 uploadVoice 上传声音');
				return;
			}
			wx.downloadVoice({
				serverId: app.voice.serverId,
				success: function (res) {
					alert('下载语音成功，localId 为' + res.localId);
					app.voice.localId = res.localId;
				}
			});
		};

		wx.config({
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
			appId: '{$appId}', // 必填，公众号的唯一标识
			timestamp: '{$config.timestamp}', // 必填，生成签名的时间戳
			nonceStr: '{$config.noncestr}', // 必填，生成签名的随机串
			signature: '{$config.signature}', // 必填，签名，见附录1
			jsApiList: [
				'startRecord',
				'stopRecord',
				'onRecordEnd',
				'playVoice',
				'pauseVoice',
				'stopVoice',
				'uploadVoice',
				'downloadVoice'
			]
		});

	})

</script>

<div class="container" id="container">
	<h3 id="menu-voice">音频接口</h3>
	<span class="desc">开始录音接口</span>
	<button class="btn btn_primary" id="startRecord">startRecord</button>
	<br>
	<span class="desc">停止录音接口</span>
	<button class="btn btn_primary" id="stopRecord">stopRecord</button>
	<br>
	<span class="desc">播放语音接口</span>
	<button class="btn btn_primary" id="playVoice">playVoice</button>
	<br>
	<span class="desc">暂停播放接口</span>
	<button class="btn btn_primary" id="pauseVoice">pauseVoice</button>
	<br>
	<span class="desc">停止播放接口</span>
	<button class="btn btn_primary" id="stopVoice">stopVoice</button>
	<br>
	<span class="desc">上传语音接口</span>
	<button class="btn btn_primary" id="uploadVoice">uploadVoice</button>
	<br>
	<span class="desc">下载语音接口</span>
	<button class="btn btn_primary" id="downloadVoice">downloadVoice</button>
</div>
<include file="./Template/footer.html" />